<?php
// $Id: tools.php 194 2012-03-06 09:55:06Z tomqin@gmail.com $

class Tools_Controller extends FrontController{

	function init(){}

	function onZoneOption(){
		$zoneid = array_get_int($_GET, 'pid', 0);
		$sel_id = array_get_int($_GET, 'zid', 0); //selected id;

		echo $this->_zoneOption($zoneid, $sel_id);
	}

	function onPinyin(){
		$str = array_get_string($_GET, 'str');
		echo json_encode($this->_pinyin($str));
	}

	function onCheckZonePinyin(){
		$city_id = array_get_int($_GET, 'city_id', 0);
		$zone_id = array_get_int($_GET, 'zone_id', 0);
		$pinyin  = array_get_string($_GET, 'pinyin');

		echo $this->_checkZonePinyin($city_id, $zone_id, $pinyin);
	}

	function onGetWaimaiTel(){
		$id = Base::crypt(array_get_string($_GET, 'code'), false);
		echo DB::getFirst('select telphone from t_store where res_id = ?', array($id));
	}

	private function _zoneOption($zoneid = 0, $sel_id = 0){
		$zone = Base::loadModule('zone', true);
		$zone->initCity();

		$str = '';
		$zones = $zone->getZoneList($zoneid);

		if($zones && is_array($zones)){
			foreach($zones as $zoneObj){
				$sel  = ($sel_id == $zoneObj->zone_id) ? ' selected' : '';
				$str .= '<option value="' .$zoneObj->zone_id. '"' .$sel. '>' .h($zoneObj->name). '</option>';
			}
		}
		return $str;
	}

	private function _pinyin($str){
		Base::loadFunction('iconv');
		$str = iconv(CHARSET, 'GBK', $str);
		return gbk_to_pinyin(trim($str));
	}

	private function _checkZonePinyin($city_id, $zone_id, $pinyin){
		return DB::getFirst("select count(zone_id) from `t_zone` where `city_id` = ? and `ename` like ? and `zone_id` <> ?",
						array(intval($city_id), $pinyin, intval($zone_id)));
	}

	function __destruct(){
		//print_r($this->db);
	}

	function onPass(){
		echo $this->dencrypt('xpower');
	}

}
?>